package persistencia;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author diegob
 */
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;



public class BD {

    protected Connection conectar() throws ClassNotFoundException, SQLException {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            return DriverManager.getConnection("jdbc:oracle:thin:@10.14.96.134:1521","A264705","A264705");
    }

    public ResultSet consultar(String sql) throws ClassNotFoundException, SQLException{
        Connection conn = conectar();
        Statement stm = conn.createStatement();
        ResultSet resultado = stm.executeQuery(sql);

        return resultado;
    }


    public void executar(String insercao, Object[] parametros, Object[] tipos) throws Exception {
        try{
                // armazena a conexão com o banco.
                Connection conn = conectar();

                PreparedStatement stm = conn.prepareStatement(insercao);
                
                for(int i=0; i<parametros.length; i++){
                   
                    if(tipos[i] == String.class){
                        if(parametros[i] != null)
                            stm.setString(i+1, (String) parametros[i]);
                        else
                            stm.setNull(i+1, Types.VARCHAR);
                    }

                    else if(tipos[i] == Integer.class){
                        if(parametros[i] != null)
                            stm.setInt(i+1, (Integer) parametros[i]);
                        else
                            stm.setNull(i+1, Types.INTEGER);
                    }

                    else if(tipos[i] == Double.class){
                        if(parametros[i] != null)
                            stm.setDouble(i+1, (Double) parametros[i]);
                        else
                            stm.setNull(i+1, Types.DOUBLE);
                    }
                    else if(tipos[i] == Date.class){
                        if(parametros[i] != null)
                            stm.setDate(i+1, (Date) parametros[i]);
                        else
                            stm.setNull(i+1, Types.DATE);
                    }
                }

                stm.executeUpdate();

        }catch(Exception e){
                e.printStackTrace();
                throw e;
        }
    }

    public ResultSet consultar(String sql, Object[] parametros, Object[] tipos) throws Exception {
        
        Connection conn = conectar();
        PreparedStatement stm = conn.prepareStatement(sql);

        for (int i = 0; i < parametros.length; i++) {

            if (tipos[i] == String.class) {
                if (parametros[i] != null) {
                    stm.setString(i + 1, (String) parametros[i]);
                } else {
                    stm.setNull(i + 1, Types.VARCHAR);
                }
            } else if (tipos[i] == Integer.class) {
                if (parametros[i] != null) {
                    stm.setInt(i + 1, (Integer) parametros[i]);
                } else {
                    stm.setNull(i + 1, Types.INTEGER);
                }
            } else if (tipos[i] == Double.class) {
                if (parametros[i] != null) {
                    stm.setDouble(i + 1, (Double) parametros[i]);
                } else {
                    stm.setNull(i + 1, Types.DOUBLE);
                }
            }

            else if(tipos[i] == Date.class){
                        if(parametros[i] != null)
                            stm.setDate(i+1, (Date) parametros[i]);
                        else
                            stm.setNull(i+1, Types.DATE);
                    }

        }


        ResultSet resultado = stm.executeQuery();

        return resultado;
    
    }
}

